<template>
	<view>

		<!--自己封装的提示器-->
		<popup ref="popup" />

		<!--商品分类选择器-->
		<u-picker :show="options.goodsCategory.show" :columns="options.goodsCategory.ops"
			@confirm="options.goodsCategory.confirm" @close="options.goodsCategory.close"
			@cancel="options.goodsCategory.close" closeOnClickOverlay="true" title="选择果蔬分类" keyName="name">
		</u-picker>

		<!-- 售卖方式选择器 -->
		<u-picker :show="options.sellMethod.show" :columns="options.sellMethod.ops"
			@confirm="options.sellMethod.confirm" @close="options.sellMethod.close" @cancel="options.sellMethod.close"
			closeOnClickOverlay="true" title="选择果蔬售卖方式" keyName="text">
		</u-picker>

		<!-- 果蔬区域选择器 -->
		<u-picker :show="options.region.show" :columns="options.region.ops" @confirm="options.region.confirm"
			@close="options.region.close" @cancel="options.region.close" closeOnClickOverlay="true" title="选择果蔬所处区域"
			keyName="name">
		</u-picker>

		<!-- 运费模板 -->
		<u-picker :show="freight.show" :columns="freight.list" @confirm="freight.confirm" @close="freight.close"
			@cancel="freight.close" closeOnClickOverlay="true" :title="freight.remark" keyName="remark">
		</u-picker>


		<!--富文本-->
		<view>
			<uni-drawer ref="showEditor" mode="left" width="410" :maskClick=false>
				<view>
					<robin-editor ref="editor" class="editor" v-model="editor.context" muiltImage="true"
						compressImage="false" :imageUploader="editorUploadImg" @cancel="editor.cancel"
						@save="editor.save" @setUrlId="setUrlId">
					</robin-editor>
				</view>
			</uni-drawer>
		</view>

		<view style="position: relative;top: -20rpx">
			<uni-section type="line" title="图文相关信息">
			</uni-section>
			<view style="margin: 0 20rpx">
				<u--form labelPosition="left" labelWidth="auto">

					<u-form-item label="果蔬标题: " borderBottom>
						<u--textarea v-model="goods.title" placeholder="输入果蔬标题" clearable autoHeight border="none"
							maxlength="100" count="true">
						</u--textarea>
					</u-form-item>

					<u-form-item label="果蔬简介:  " borderBottom>
						<u--textarea v-model="goods.overview" placeholder="果蔬简介" clearable autoHeight border="none"
							maxlength="255" count="true">
						</u--textarea>
					</u-form-item>

					<u-form-item label="果蔬分类:  " borderBottom>
						<u-tag class="my-tag" type="success" :text=options.goodsCategory.text plain
							@click="options.goodsCategory.show = true">
						</u-tag>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>

					<!--     <u-form-item label="果蔬主图: " borderBottom>
            <uni-file-picker
                ref="avatarFile"
                fileMediatype="image"
                mode="grid"
                @select="goods.avatar.select"
                @delete="goods.avatar.delete"
                :image-styles="upload.style"
                limit="1"
                v-model="goods.avatar.list"
            >
              <u-icon name="photo" color="#42535f" size="40"></u-icon>
            </uni-file-picker>
          </u-form-item> -->

					<u-form-item label="上传图片: " borderBottom>
						<uni-file-picker ref="imgFile" fileMediatype="image" mode="grid" @select="goods.img.select"
							@delete="goods.img.delete" :image-styles="upload.style" limit="10" v-model="goods.img.list">
							<u-icon name="photo" color="#42535f" size="40"></u-icon>
						</uni-file-picker>
					</u-form-item>

					<!--  <u-form-item label="果蔬详情(图文并茂) " borderBottom>
            <u-button
                type="success"
                shape="circle"
                plain
                text="打开编辑窗"
                @click="editor.open"
            >
            </u-button>
          </u-form-item> -->

					<u-form-item label="果蔬视频:" borderBottom>
						<view>
							<video v-if="!service.check.isNull(goods.video.url)" style="width: 100px;
                  height: 100px;
                  display: inline-block;
                  " id="myVideo" :src=goods.video.url :enable-danmu="false" :danmu-btn="false" :direction="0" controls>
							</video>
							<view style="width: 200px;display: inline-block;margin-left: 15px">
								<uni-file-picker limit="1" fileMediatype="video" mode="list"
									@select="goods.video.select" @delete="goods.video.reDefault">
									<u-button>
										{{service.check.isNull(goods.video.url) ? goods.video.text1 : goods.video.text2}}
									</u-button>
								</uni-file-picker>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="是否可发快递:" borderBottom>
						<view style="display: flex;">
							<u-switch v-model="hasCourier" @change="change" :inactiveValue="-1" :activeValue="1">
							</u-switch>

							<view style="width: 70%;margin-left: auto;" v-show="hasCourier==1">
								<u-tag class="my-tag" type="success" :text="freight.remark" plain
									@click="freight.show = true">
								</u-tag>

							</view>
							<u-icon slot="right" name="arrow-right" v-if="hasCourier==1"></u-icon>

						</view>

					</u-form-item>
				</u--form>

			</view>

			<uni-section type="line" title="售卖相关信息">
			</uni-section>
			<view style="margin: 0 20rpx">

				<u--form labelPosition="left" labelWidth="auto">

					<u-form-item label="所处区域:  " borderBottom>
						<u-tag class="my-tag" type="warning" :text=options.region.text plain
							@click="options.region.show = true">
						</u-tag>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>

					<u-form-item label="售卖方式:  " borderBottom>
						<u-tag class="my-tag" :text=options.sellMethod.text plain
							@click="options.sellMethod.show = true">
						</u-tag>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>

					<u-form-item label="推广佣金(单位/元) :" borderBottom>
						<u--input v-model="goods.commission" type="digit" placeholder="输入推广佣金">
						</u--input>
					</u-form-item>

					<view v-if="goods.type === 2 || goods.type === 0">
						<u-form-item label="采摘价格(单位/元) :" borderBottom>
							<u--input v-model="goods.price" type="digit" placeholder="输入采摘价格">
							</u--input>
						</u-form-item>

						<u-form-item label="采摘库存(-1为无限库存) :" borderBottom>
							<u-number-box v-model="goods.inventory" :step="1" :min="-1" :max="999999" bgColor="#ffffff"
								inputWidth="80px">
							</u-number-box>
						</u-form-item>
					</view>

					<view v-if="goods.type === 2 || goods.type === 1">
						<u-form-item label="种植价格(单位/元) :" borderBottom>
							<u--input v-model="goods.growPrice" type="digit" placeholder="输入种植价格">
							</u--input>
						</u-form-item>

						<u-form-item label="种植库存(-1为无限库存) :" borderBottom>
							<u-number-box v-model="goods.growInventory" :step="1" :min="-1" :max="999999"
								bgColor="#ffffff" inputWidth="80px">
							</u-number-box>
						</u-form-item>
					</view>

				</u--form>
			</view>

			<view>
				<view style="margin: 10px">
					<u-button type="primary" shape="circle" @click="commit">
						<text style="font-size: 18px;font-weight: bold">提 交</text>
					</u-button>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import service from '@/utils/service';
	import {
		pathToBase64
	} from "@/js_sdk/mmmm-image-tools/index";
	import popup from "@/merchant/my/my-popup"
	import avatar from "../../../uni_modules/uview-ui/libs/config/props/avatar";
	export default {
		components: {
			popup
		},
		data() {
			let _vm = this
			return {
				hasCourier: -1,
				/* 跳转到当前页面的目标 默认: 添加*/
				target: 'add',
				service: service,
				/* 商品信息 */
				goods: {
					id: '',
					/* 商品标题 */
					title: '',
					/* 分类id */
					categoryId: '',
					/* 商品简介 */
					overview: '',
					/* 售卖方式*/
					type: '',
					/* 区域id */
					regionId: '',
					/* 果蔬主图 */
					avatar: {
						//页面显示用 元素 {url: '',base64: ''}
						list: [],
						reDefault() {
							this.list = [];
						},
						// 选中图像
						select(e) {
							let _t = this;
							let avatar = this.goods.avatar;
							console.log('选择商品主图 ：', e);
							let size = 10;
							if (service.check.fileSizeOutBounds(e.tempFiles, size)) {
								_t.$refs.popup.error('不能上传超过 ' + size + ' M 的图像')
								// 清空
								_t.$refs.avatarFile.clearFiles(0);
								avatar.reDefault();
								return
							}
							pathToBase64(e.tempFilePaths[0]).then(res => {
								avatar.list.push({
									url: e.tempFilePaths[0],
									base64: res
								});
							});
						},
						// 删除选中头像
						delete(e) {
							console.log("删除商品主图 :", e)
							this.goods.avatar.reDefault();
						}
					},
					/* 果蔬轮播图 */
					img: {
						// 页面显示用 元素: {url: '',base64:''}
						list: [],
						// 轮播图选中
						select(e) {
							let _t = this;
							let img = this.goods.img;
							console.log("选中轮播图 : ", e)
							let size = 10;
							if (service.check.fileSizeOutBounds(e.tempFiles, size)) {
								_t.$refs.popup.error('不能上传超过 ' + size + ' M 的图像')
								// 删除显示图片的下标为 当前列表的长度 即 即将添加的图片下标
								_t.$refs.imgFile.clearFiles(img.list.length);
								return
							}
							// 解析成 base 64 添加到列表中
							e.tempFilePaths.forEach(e => {
								pathToBase64(e).then(res => {
									img.list.push({
										url: e,
										base64: res
									})
								});
							})
							console.log("选中后的图像列表 : ", img.list)
						},
						// 轮播图删除
						delete(e) {
							let _t = this;
							let img = this.goods.img;
							console.log("即将删除的轮播图 :", e)
							console.log("即将操作的轮播图列表 : ", img.list)
							// 得到将要删除的图像 url
							let delUrl = e.tempFilePath;
							// 得到需要删除的下标
							let index = img.list.findIndex(v => {
								return delUrl === v.url;
							})
							console.log("即将删除的轮播图下标 : ", index)
							img.list.splice(index, 1);
							console.log("删除后的轮播列表: ", img.list)
						}
					},
					/*果蔬详情 富文本*/
					content: '',
					/*果蔬视频*/
					video: {
						text1: '上传视频',
						text2: '重新上传',
						url: '',
						hasUpdate: false,
						reDefault() {
							_vm.goods.video.url = '';
							_vm.goods.video.hasUpdate = true;
						},
						// 选中文件
						select(v) {
							console.log("选中文件 : ", v)
							if (service.check.fileSizeOutBounds(v.tempFiles, 50)) {
								_vm.$refs.popup.error("上传视频文件大小不能超过 50 M")
								return
							}
							_vm.goods.video.url = v.tempFilePaths[0]
							_vm.goods.video.hasUpdate = true;
						}
					},
					/* 采摘价格 */
					price: 1.00,
					/* 采摘库存 */
					inventory: -1,
					/* 种植价格 */
					growPrice: 1.00,
					/* 种植库存 */
					growInventory: -1,
					/* 推广佣金 */
					commission: 0,
					/* 静态资源路径id */
					urlId: '',
				},
				/* 土地详情 */
				plotDetail: null,
				freight: {
					remark: "选择运费模板",
					id: "",
					show: false,
					list: [
						[]
					],
					confirm(e) {
						this.freight.id = e.value[0].id
						this.freight.remark = e.value[0].remark
						this.freight.show = false
					},
					close(e) {
						this.freight.show = false
					},
					defSelect(categoryId) {
						console.log("this.freight111: ", _vm.freight);

						let item = _vm.freight.list[0].find(e => {
							return e.id === categoryId
						})
						console.log("categoryId: ", categoryId);
						console.log("item: ", item);
						_vm.freight.remark = item.remark
					},
				},
				/* 选项对象 */
				options: {
					/* 商品分类 */
					goodsCategory: {
						show: false,
						text: "点击选择果蔬分类",
						ops: [
							[]
						],
						open() {
							_vm.options.goodsCategory.show = true
						},
						close() {
							_vm.options.goodsCategory.show = false
						},
						def() {
							_vm.options.goodsCategory.text = "点击选择果蔬分类"
						},
						defSelect(categoryId) {
							let item = _vm.options.goodsCategory.ops[0].find(e => {
								return e.id === categoryId
							})
							console.log("默认选中的分类: ", item)
							_vm.options.goodsCategory.text = item.name
						},
						confirm(v) {
							console.log("点击选择商品分类 :", v)
							_vm.goods.categoryId = v.value[0].id;
							_vm.options.goodsCategory.text = v.value[0].name;
							_vm.options.goodsCategory.close();
						}
					},
					/* 商品的售卖方式 */
					sellMethod: {
						show: false,
						text: '点击选择果蔬的售卖方式',
						ops: [
							[{
									v: 0,
									text: '采摘'
								},
								{
									v: 1,
									text: '种植'
								},
								{
									v: 2,
									text: '采/种 均支持'
								},
							]
						],
						open() {
							_vm.options.sellMethod.show = true
						},
						close() {
							_vm.options.sellMethod.show = false
						},
						def() {
							_vm.options.sellMethod.text = "点击选择果蔬的售卖方式"
						},
						defSelect(type) {
							// 得到默认选中的属性
							let item = _vm.options.sellMethod.ops[0].find(e => {
								return e.v === type
							})
							if (!service.check.isNull(item)) {
								console.log("默认选中的售卖方式: ", item)
								_vm.options.sellMethod.text = item.text
							} else {
								// 如果没有选中把 商品支持的售卖方式制空
								_vm.goods.type = "";
							}
						},
						confirm(v) {
							console.log("点击选择商品的售卖方式 :", v)
							_vm.goods.type = v.value[0].v;
							_vm.options.sellMethod.text = v.value[0].text;
							_vm.options.sellMethod.close();
						}
					},

					/* 果蔬区域选项 */
					region: {
						show: false,
						text: '点击选择果蔬区域',
						ops: [
							[]
						],
						open() {
							_vm.options.region.show = true
						},
						close() {
							_vm.options.region.show = false
						},
						def() {
							_vm.options.region.text = "点击选择果蔬区域"
						},
						defSelect(regionId) {
							// 得到默认选中的属性
							let item = _vm.options.region.ops[0].find(e => {
								return e.id === regionId
							})
							if (!service.check.isNull(item)) {
								console.log("默认选中的果蔬区域方式: ", item)
								_vm.options.region.text = item.name
							} else {
								// 如果没有选中把 商品支持的售卖方式制空
								_vm.goods.regionId = "";
							}
						},
						confirm(v) {
							console.log("选中的果蔬区域方式 :", v)
							_vm.goods.regionId = v.value[0].id;
							_vm.options.region.text = v.value[0].name;
							_vm.options.region.close();
						}
					}

				},
				/* 加载对象 */
				loading: {
					// 进度
					plan: 0,
					// 阈值
					threshold: 0,
					step() {
						try {
							if (++this.plan >= this.threshold) {
								// 根据土地支持的售卖方式 限制售卖方式选项
								if (!service.check.isNull(_vm.plotDetail)) {
									let businessType = _vm.plotDetail.businessType;
									/* 全支持不处理 */
									if (businessType !== 2) {
										let item = _vm.options.sellMethod.ops[0].find(e => {
											return e.v === businessType
										})
										_vm.options.sellMethod.ops[0] = [item]
										console.log(_vm.options.sellMethod.ops[0])
										if (!service.check.isNull(item)) {
											_vm.options.sellMethod.text = item.text;
											_vm.goods.type = item.v;
										}
									}
								}

								// 回显
								if (_vm.target === 'update') {
									console.log("_vm: ", _vm);
									// 分类
									_vm.options.goodsCategory.defSelect(_vm.goods.categoryId)
									// 售卖方式
									_vm.options.sellMethod.defSelect(_vm.goods.type)
									// 区域
									_vm.options.region.defSelect(_vm.goods.regionId)
									_vm.freight.defSelect(_vm.freight.freightId)

								}
							}
						} finally {
							uni.hideLoading();
						}

					}
				},
				/*文件上传相关对象*/
				upload: {
					style: {
						"height": 80, // 边框高度
						"width": 80, // 边框宽度
						"border": { // 如果为 Boolean 值，可以控制边框显示与否
							"color": "#3646505e", // 边框颜色
							"width": "3px", // 边框宽度
							"style": "solid", // 边框样式
						},
					},
				},
				/*富文本对象*/
				editor: {
					// 空文本展位
					placeholder: '请输入商品详情',
					// 富文本内容
					context: '',
					// 富文本临时内容
					tempText: '',
					// 初始值
					initial: "<p><br></p>",
					open() {
						this.$refs.showEditor.open()
					},
					save(e) {
						console.log("富文本保存事件 : ", e)
						// 如果跟初始值一致则不进行赋值
						if (e.html !== _vm.editor.initial) {
							_vm.editor.context = service.copy(e.html)
							_vm.goods.content = service.copy(e.html)
						}
						_vm.editor.close();
					},
					// 取消则测回修改
					cancel(e) {
						_vm.editor.close();
					},
					close() {
						_vm.$refs.showEditor.close()
					},
				},
			};
		},
		methods: {
			change(e) {
				console.log("e: ", e);
			},
			/*设置资源路径id*/
			setUrlId(v) {
				this.goods.urlId = v;
			},
			/* 富文本上传图像 */
			editorUploadImg(path, callback, _t = this) {
				// 获取图像地址前缀
				let imgApi = this.$reqConfig.URLIMAGE
				// 获取请求头类型
				this.contentType = this.$reqConfig.CONTENTTYPE + ";+charset=UTF-8"
				// 获取token
				this.token = uni.getStorageSync("merchantsToken").token
				uni.uploadFile({
					url: this.$reqConfig.UPLOADAPI + 'merchant/merchantGroupGoods/uploadImg',
					filePath: path,
					name: 'file',
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
					success: res => {
						let result = JSON.parse(res.data);
						if (result.code === 0) {
							callback(imgApi + result.result.uploads[0])
							this.$emit('setUrlId', result.result.id)
						}
					},
				})
			},
			/* 获取商品详情 */
			getDetail() {
				let _t = this;
				this.$axios({
					url: "merchant/merchantVegetable/detail",
					method: "post",
					data: {
						id: _t.goods.id,
					},
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
				}).then(res => {
					if (res.code === 0) {
						console.log("res: ", res);
						// 解析错误的字段
						let errorFields = [];
						service.executeTask(function() {
							_t.goods.title = res.result.title
						}, errorFields, "果蔬标题")

						service.executeTask(function() {
							_t.goods.overview = res.result.overview;
						}, errorFields, "果蔬简介")

						service.executeTask(function() {
							_t.goods.categoryId = res.result.categoryId
						}, errorFields, "果蔬分类")

						service.executeTask(function() {
							_t.goods.avatar.list.push({
								url: _t.imgApi + res.result.avatar,
								base64: ''
							})
						}, errorFields, "果蔬主图")

						service.executeTask(function() {
							JSON.parse(res.result.img).forEach(e => {
								_t.goods.img.list.push({
									url: _t.imgApi + e,
									base64: ''
								})
							})
						}, errorFields, "果蔬轮播图")

						service.executeTask(function() {
							if (!service.check.isNull(res.result.video)) {
								_t.goods.video.url = _t.imgApi + res.result.video
							}
						}, errorFields, "果蔬视频")

						service.executeTask(function() {
							if (!res.result.content.includes("style")) {
								res.result.content = res.result.content.replace(/\<img/gi,
									'<img style="max-width:100%;height:auto"')
							}
							if (!res.result.content.includes(_t.imgApi)) {
								res.result.content = res.result.content
									.replaceAll(/src="/gi, 'src=' + '\"' + _t.imgApi);
							}
							console.log(res.result.content)
							_t.goods.content = service.copy(res.result.content);
							_t.editor.context = service.copy(res.result.content);
						}, errorFields, "果蔬详情")

						service.executeTask(function() {
							_t.goods.type = parseInt(res.result.type)
						}, errorFields, "售卖方式")

						service.executeTask(function() {
							_t.goods.regionId = res.result.regionId
						}, errorFields, "所处区域")

						service.executeTask(function() {
							_t.goods.price = res.result.price
						}, errorFields, "采摘价格")

						/*service.executeTask(function () {
						  _t.goods.inventory = res.result.inventory
						},errorFields,"采摘库存")*/

						service.executeTask(function() {
							_t.goods.growPrice = res.result.growPrice
						}, errorFields, "种植价格")

						/*service.executeTask(function () {
						  _t.goods.growInventory = res.result.growInventory
						},errorFields,"种植库存")*/

						service.executeTask(function() {
							_t.goods.commission = res.result.commission
						}, errorFields, "推广佣金")

						if (_t.$comm.checkNull(res.result.hasCourier)) {
							service.executeTask(function() {
								_t.hasCourier = res.result.hasCourier
							}, errorFields, "推广佣金")
						}


						if (_t.$comm.checkNull(res.result.freightId)) {
							service.executeTask(function() {
								_t.freight.freightId = res.result.freightId
							}, errorFields, "推广佣金")
						}
						service.executeTask(function() {
							_t.goods.urlId = res.result.urlId
						}, errorFields, "推广佣金")



						if (errorFields.length !== 0) {
							_t.$refs.popup.error("获取失败的参数 : " + errorFields.toString())
						}
					}
				}).finally(() => {
					_t.loading.step();
				})
			},
			/* 获取商品分类选项 */
			getCategoryOptions() {
				let _t = this
				this.$axios({
					url: "system/sysCategory/listNoPage",
					method: "post",
					data: {
						type: 2
					},
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
				}).then(res => {
					console.log(" system/sysCategory/listNoPage : ", res)
					if (res.code === 0) {
						_t.options.goodsCategory.ops[0] = res.result;
					}
				}).finally(() => {
					_t.loading.step();
				});
			},
			/* 获取所属菜地区域 */
			getRegionList() {
				let _t = this;
				this.$axios({
					url: "merchant/merchantVegetable/regionList",
					method: "post",
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
				}).then(res => {
					if (res.code === 0) {
						_t.options.region.ops[0] = res.result;
					}
				}).finally(() => {
					_t.loading.step();
				})
			},
			//运费模板列表
			getfreightTemplate() {
				let _t = this;
				this.$axios({
					url: "merchant/merchantFreightTemplate/list",
					method: "post",
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
				}).then(res => {
					if (res.code === 0) {
						this.freight.list[0] = res.result;
						console.log("this.freight: ", this.freight);
					}
				}).finally(() => {
					_t.loading.step();
				})
			},
			/* 获取土地详情 */
			getPlotDetail() {
				let _t = this
				this.$axios({
					url: "merchant/merchantVegetablePlot/detail",
					method: "post",
					data: {
						id: -1
					},
					header: {
						"Authorization": this.token,
						"Content-type": this.contentType,
						"type": "merchantsToken"
					},
				}).then(res => {
					console.log(" merchant/merchantVegetablePlot/detail: ", res)
					if (res.code === 0) {
						_t.plotDetail = res.result;
					}
				}).finally(() => {
					_t.loading.step();
				})
			},
			/* 校验参数 */
			checkParam() {
				let goods = this.goods;
				let check = service.check;
				let _t = this;
				if (check.isNull(goods.title)) {
					_t.$refs.popup.error("请输入果蔬标题")
					return true;
				}

				if (check.isNull(goods.overview)) {
					_t.$refs.popup.error("请输入果蔬简介")
					return true;
				}

				if (check.isNull(goods.categoryId)) {
					_t.$refs.popup.error("请选择果蔬分类")
					return true;
				}

				// if(check.isNull(goods.avatar.list)){
				//   _t.$refs.popup.error("请上传果蔬主图")
				//   return true;
				// }

				if (check.isNull(goods.img.list)) {
					_t.$refs.popup.error("请至少上传一张果蔬轮播图")
					return true;
				}

				// if(check.isNull(goods.content)){
				//   _t.$refs.popup.error("请编辑果蔬详情")
				//   return true;
				// }

				if (goods.video.hasUpdate && check.isNull(goods.video.url)) {
					_t.$refs.popup.error("上传的果蔬视频丢失,请尝试重新上传")
					return true;
				}

				if (check.isNull(goods.type)) {
					_t.$refs.popup.error("请选择售卖方式")
					return true;
				}

				if (check.isNull(goods.regionId)) {
					_t.$refs.popup.error("请选择果蔬所处区域")
					return true;
				}

				if (!check.isNumber(goods.commission)) {
					_t.$refs.popup.error("请输入推广佣金")
					return true;
				} else if (check.isDecimalOutBounds(goods.commission, 2)) {
					_t.$refs.popup.error("请调整推广佣金 [金额只能精确到 '分' ( 即保留只两位小数 )]")
					return true;
				} else if (goods.commission < 0) {
					_t.$refs.popup.error("请调整推广佣金 金额不得小于 0 元")
					return true
				}

				if (goods.type === 2 || goods.type === 0) {
					if (!check.isNumber(goods.price)) {
						_t.$refs.popup.error("请输入采摘价格")
						return true;
					} else if (check.isDecimalOutBounds(goods.price, 2)) {
						_t.$refs.popup.error("请调整采摘价格 [金额只能精确到 '分' ( 即保留只两位小数 )]")
						return true;
					} else if (goods.price <= 0) {
						_t.$refs.popup.error("请调整采摘价格 价格必须 大于 0 元")
						return true
					}
					/*if (!check.isNumber(goods.inventory)) {
					  _t.$refs.popup.error("请输入采摘库存")
					  return true;
					}else if(goods.inventory < -1){
					  _t.$refs.popup.error("采摘库存至少为 -1")
					  return true;
					}*/
				}

				if (goods.type === 2 || goods.type === 1) {
					if (!check.isNumber(goods.growPrice)) {
						_t.$refs.popup.error("请输入种植价格")
						return true;
					} else if (check.isDecimalOutBounds(goods.growPrice, 2)) {
						_t.$refs.popup.error("请调整种植采摘价格 [金额只能精确到 '分' ( 即保留只两位小数 )]")
						return true;
					} else if (goods.growPrice <= 0) {
						_t.$refs.popup.error("请调整种植采摘价格 价格必须 大于 0 元")
						return true
					}
					/*if (!check.isNumber(goods.growInventory)) {
					  _t.$refs.popup.error("请输入种植库存")
					  return true;
					}else if(goods.growInventory < -1){
					  _t.$refs.popup.error("种植库存至少为 -1")
					  return true;
					}*/

				}
				if (this.hasCourier == 1) {
					if (!this.$comm.checkNull(this.freight.id)) {
						_t.$refs.popup.error("请选择运费模板，如没有请到运费管理处添加")
						return true
					}
				}
				return false;
			},

			/* 提交信息 */
			commit() {
				let _t = this;
				if (this.checkParam()) {
					return;
				}
				uni.showLoading({
					title: '正在提交中....',
					mask: true
				});
				if (this.target === 'add') {
					if (this.goods.video.hasUpdate && !service.check.isNull(this.goods.video.url)) {
						uni.uploadFile({
							url: _t.$config.UPLOADAPI + 'merchant/merchantVegetable/addVegetable',
							filePath: _t.goods.video.url,
							name: 'video',
							formData: _t.getAddRequest(),
							header: {
								"Authorization": this.token,
								"Content-type": this.contentType,
								"type": "merchantsToken"
							},
							success: (res) => {
								console.log("merchant/merchantVegetable/addVegetable : ", JSON.parse(res
									.data));
								if (JSON.parse(res.data).code === 0) {
									_t.$refs.popup.success('添加果蔬成功');
									uni.navigateBack({
										delta: 1
									})
								} else {
									_t.$refs.popup.success('添加商品果蔬 : ' + JSON.parse(res.data).message);
								}
							},
							complete: (res) => {
								uni.hideLoading()
							}
						});
					} else {
						this.$axios({
							url: 'merchant/merchantVegetable/addVegetable',
							method: 'post',
							data: _t.getAddRequest(),
							header: {
								"Authorization": this.token,
								"Content-type": this.contentType,
								"type": "merchantsToken"
							},
						}).then(res => {
							console.log("merchant/merchantVegetable/addVegetable : ", res);
							if (res.code === 0) {
								_t.$refs.popup.success('添加果蔬成功');
								uni.navigateBack({
									delta: 1
								})
							} else {
								_t.$refs.popup.error('添加果蔬失败 : ' + res.message);
							}
						}).finally(() => {
							uni.hideLoading()
						})
					}
				} else if (this.target === 'update') {
					this.updateRequest();
				}
			},
			/* 修改果蔬请求 */
			updateRequest() {
				let _t = this;
				if (this.hasCourier == 1) {
					if (!this.$comm.checkNull(this.freight.id)) {
						_t.$refs.popup.error("请选择运费模板，如没有请到运费管理处添加")
						return true
					}
				}
				if (this.goods.video.hasUpdate && !service.check.isNull(this.goods.video.url)) {
					uni.uploadFile({
						url: _t.$config.UPLOADAPI + 'merchant/merchantVegetable/updateVegetable',
						filePath: _t.goods.video.url,
						name: 'video',
						formData: _t.getUpdateRequest(),
						header: {
							"Authorization": this.token,
							"Content-type": this.contentType,
							"type": "merchantsToken"
						},
						success: (res) => {
							console.log("merchant/merchantVegetable/updateVegetable : ", JSON.parse(res.data));
							if (JSON.parse(res.data).code === 0) {
								_t.$refs.popup.success('修改果蔬成功');
								uni.navigateBack({
									delta: 1
								})
							} else {
								_t.$refs.popup.error('修改果蔬失败 : ' + JSON.parse(res.data).message);
							}
						},
						complete: (res) => {
							uni.hideLoading()
						}
					});
				} else {
					this.$axios({
						url: 'merchant/merchantVegetable/updateVegetable',
						method: 'post',
						data: _t.getUpdateRequest(),
						header: {
							"Authorization": this.token,
							"Content-type": this.contentType,
							"type": "merchantsToken"
						},
					}).then(res => {
						console.log("merchant/merchantVegetable/updateVegetable : ", res);
						if (res.code === 0) {
							_t.$refs.popup.success('修改果蔬成功');
							uni.navigateBack({
								delta: 1
							})
						} else {
							_t.$refs.popup.error('修改果蔬失败 : ' + res.message);
						}
					}).finally(() => {
						uni.hideLoading()
					})
				}
			},
			/* 添加请求参数 */
			getAddRequest() {
				let goods = service.copy(this.goods);
				goods.content = goods.content.replaceAll(this.imgApi, "");
				let data = {
					title: goods.title,
					categoryId: goods.categoryId,
					// avatar: goods.avatar.list[0].base64,
					type: goods.type,
					regionId: goods.regionId,
					img: JSON.stringify(goods.img.list.map(e => {
						return e.base64
					})),
					overview: goods.overview,
					// content: goods.content,
					inventory: goods.inventory,
					price: goods.price,
					growPrice: goods.growPrice,
					growInventory: goods.growInventory,
					commission: goods.commission,
					urlId: goods.urlId,
					app: "wx",
					hasCourier: this.hasCourier,
					freightId: this.freight.id
				};
				console.log("即将发送 添加果蔬请求的参数 : ", data)
				return data;
			},
			/* 修改请求参数 */
			getUpdateRequest() {
				let _t = this;
				let goods = service.copy(this.goods);
				goods.content = goods.content.replaceAll(this.imgApi, "");
				// 处理轮播图
				let img = [];
				goods.img.list.forEach(e => {
					if (!service.check.isNull(e.base64)) {
						img.push(e.base64)
					} else if (!service.check.isNull(e.url)) {
						img.push(e.url.replace(_t.imgApi, ""))
					}
				})
				// // 处理主图
				// let avatar = service.check.isNull(goods.avatar.list[0].base64) ?
				//     goods.avatar.list[0].url.replace(_t.imgApi, "") : goods.avatar.list[0].base64;
				let data = {
					id: goods.id,
					title: goods.title,
					categoryId: goods.categoryId,
					overview: goods.overview,
					// avatar: avatar,
					img: JSON.stringify(img),
					// content: goods.content,
					inventory: goods.inventory,
					price: goods.price,
					growPrice: goods.growPrice,
					growInventory: service.check.isNull(goods.growInventory) ? -1 : goods.growInventory,
					type: goods.type,
					regionId: goods.regionId,
					hasVideo: goods.video.hasUpdate.toString(),
					commission: goods.commission,
					app: "wx",
					hasCourier: this.hasCourier,
					freightId: this.freight.id
				}
				console.log("即将发送 修改果蔬请求的参数 : ", data)
				return data;
			}
		},
		onLoad(e) {
			console.log("路由传参的值 : ", e)
			// 获取图像地址前缀
			this.imgApi = this.$reqConfig.URLIMAGE
			// 获取请求头类型
			this.contentType = this.$reqConfig.CONTENTTYPE + ";+charset=UTF-8"
			// 获取token
			this.token = uni.getStorageSync("merchantsToken").token
			uni.showLoading({
				title: '获取数据中....',
				mask: true
			})
			// 初始化加载中 阈值
			this.loading.threshold = 3;
			if (!service.check.isNull(e.param)) {
				let route = JSON.parse(e.param);
				if (route.target === 'update') {
					this.goods.id = route.id;
					this.target = route.target;
					// 追加阈值
					this.loading.threshold++;
					this.getDetail();
				}
			}
			this.getCategoryOptions();
			this.getPlotDetail();
			this.getRegionList();
			this.getfreightTemplate()
		}
	}
</script>

<style lang="scss">
	/*表单下划线*/
	.u-line.data-v-727e452e {
		border-bottom-width: 8rpx !important;
	}

	/deep/.uni-section__content-title.data-v-5584ec96 {
		font-size: 34rpx !important;
	}

	/*表单 label*/
	.u-form-item__body__left__content__label.data-v-067e4733 {
		font-size: 30rpx;
		font-weight: bold;
	}

	/*标题分割线样式*/
	/deep/ .line.data-v-5584ec96 {
		height: 15px;
		background-color: #0dd8ae !important;
		border-radius: 5px;
		width: 7px !important;
	}

	.my-tag {
		z-index: 1 !important;
	}

	.uni-drawer {
		z-index: 9999 !important;
	}
</style>
